import processing.core.*;
import processing.xml.*;

import processing.opengl.*;

import java.applet.*;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import java.awt.Image;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;
import java.util.regex.*;

public class Sphere {

	PImage world;

	public void setup(PApplet pApplet) {
		// size(1024, 768, OPENGL);
		// frameRate(25);
		// smooth();
		world = pApplet.loadImage("Mercator-projection.jpg");
	}

	float a = 0;

	public void draw(PApplet pApplet) {
		// background(0);
		pApplet.pushMatrix();
		pApplet.pushStyle();
		pApplet.lights();
		pApplet.noStroke();
		pApplet.fill(128);
		pApplet.translate(pApplet.mouseX, pApplet.mouseY);

		pApplet.rotateX(PApplet.radians(-30));
		pApplet.rotateY(a);
		a += 0.01f;
		makeSphere(100, 10, world, pApplet);

		pApplet.popMatrix();
		pApplet.popStyle();
	}

	public void makeSphere(int R, int step, PImage tex, PApplet pApplet) {
		
		
		pApplet.beginShape(PApplet.TRIANGLE_STRIP);
		pApplet.texture(tex);
		for (int i = 0; i < 90; i += step) {
			float sini = PApplet.sin(PApplet.radians(i));
			float cosi = PApplet.cos(PApplet.radians(i));
			float sinip = PApplet.sin(PApplet.radians(i + step));
			float cosip = PApplet.cos(PApplet.radians(i + step));

			for (int j = 0; j < 360; j += step) {
				float sinj = PApplet.sin(PApplet.radians(j));
				float cosj = PApplet.cos(PApplet.radians(j));
				float sinjp = PApplet.sin(PApplet.radians(j + step));
				float cosjp = PApplet.cos(PApplet.radians(j + step));

				// Upper hemisphere
				pApplet.vertex(R * cosj * sini, R * -cosi, R * sinj * sini, // x,
																			// y,
																			// z
						tex.width - j * tex.width / 360, i * tex.height / 180 // u,
				// v
						);

				pApplet.vertex(R * cosjp * sini, R * -cosi, R * sinjp * sini,
						tex.width - (j + step) * tex.width / 360, i
								* tex.height / 180);

				pApplet.vertex(R * cosj * sinip, R * -cosip, R * sinj * sinip,
						tex.width - j * tex.width / 360, (i + step)
								* tex.height / 180);

				pApplet.vertex(R * cosjp * sinip, R * -cosip,
						R * sinjp * sinip, tex.width - (j + step) * tex.width
								/ 360, (i + step) * tex.height / 180);

				// Lower hemisphere
				pApplet.vertex(R * cosj * sini, R * cosi, R * sinj * sini,
						tex.width - j * tex.width / 360, tex.height - i
								* tex.height / 180);

				pApplet.vertex(R * cosjp * sini, R * cosi, R * sinjp * sini,
						tex.width - (j + step) * tex.width / 360, tex.height
								- i * tex.height / 180);

				pApplet.vertex(R * cosj * sinip, R * cosip, R * sinj * sinip,
						tex.width - j * tex.width / 360, tex.height
								- (i + step) * tex.height / 180);

				pApplet.vertex(R * cosjp * sinip, R * cosip, R * sinjp * sinip,
						tex.width - (j + step) * tex.width / 360, tex.height
								- (i + step) * tex.height / 180);
			}
		}
		pApplet.endShape();
	}

	static public void main(String args[]) {
		PApplet.main(new String[] { "--present", "--bgcolor=#666666",
				"--stop-color=#cccccc", "Sphere" });
	}
}
